#!/usr/bin/perl -w
#
# weblog-filter.pl - filter code and code-containing HTML for posting to K5
#
# $Id: weblog-filter.pl,v 1.5 2001/12/17 02:06:58 thor Exp $
#
# Tom Moertel <tk5 /a/t/ moertel.com>
# 30 July 2001
#
# Filters a block of code or HTML possibly mixed with code into
# something suitable posting to kuro5hin.org, which doesn't have
# support for PRE and other elements that are useful for presenting
# code in HTML.
#
# If your input text contains PRE blocks, only they will be processed;
# otherwise, the entire text will be processed.

use strict;

sub fixup($) {

    local $_ = $_[0];

    # escape characters that could be confused for HTML
    # character-entity references (note that order is important here)

    s/&/&amp;/g;                   # escape & chars
    s/</&lt;/g;                    # escape < chars

    # perform markup conversions

    s/(\r\n?|\n)/<br>$1/sg;        # lineends    -> <br>
    s/\t/    /g;                   # tabs        -> 4 spaces
    s/ (?= )/&nbsp;/g;             # space runs  -> &nbsp;
    s/^ /&nbsp;/mg;                # leading sp  -> &nbsp;
    s/&nbsp;&nbsp;(?=&nbsp;)/&nbsp; /g;

    # package it all up inside of TT

    return "<tt>$_</tt>";
}


# MAIN

undef $/;  # read input in one big chunk
$_ = <>;
$_ = fixup($_) unless s{<pre>(.*?)</pre>}{fixup($1)}gsie;
print;
